
#include	"Enemy.h"
#include	"defs.h"
#include	"Util.h"
#include	"SpaceShooter.h"

Enemy::Enemy(){
	timer = 0;
}

Enemy::Enemy(const Enemy& e) : Ship(e) {

	shipSpeed = e.shipSpeed;
	eShipType = e.eShipType;

	weapon = e.weapon;

	weapon.cleanProjectiles();

	weapon.setShip(this);

	timer = 0;
}

bool Enemy::init(char* shipImage, eShipTypes st){

	if(!Object::init(shipImage))
		return false;

	eShipType = st;

	weapon.setEnemyWeapon(true);

	switch(eShipType)
	{
		case ESHIP_1:

			if(!weapon.init(Weapon::PULSE_GUN, this))
				return false;

			collisionRect.x = 11;
			collisionRect.y = 4;
			collisionRect.w = 38;
			collisionRect.h = 39;

			shipHealth = 10;

			//weapon.setFireRate(Weapon::PULSE_GUN_RATE);
			weapon.getProjectile()->setSpeedY(10); //default is 12

			shipSpeed = ESHIP_1_SPEED;
			shipState = SHIP_FORWARD;

			speedY_multiplier = 1.1f;
			speedX_multiplier = 3.0f;

			weapon.isFiring = true;

		break;

		case ESHIP_2:

			if(!weapon.init(Weapon::LASER_GUN, this))
				return false;

			collisionRect.x = 6;
			collisionRect.y = 4;
			collisionRect.w = 49;
			collisionRect.h = 42;

			shipHealth = 14;

			shipSpeed = ESHIP_2_SPEED;
			shipState = SHIP_FORWARD;

			speedY_multiplier = 0.7f;
			speedX_multiplier = 1.2f;

			weapon.getProjectile()->setSpeedY(10); //default is 12

			weapon.isFiring = true;

		break;

		default:
			return false;
	}
	
	return true;
}

void Enemy::loop(){

	moveEnemy();

	if( shipState & SHIP_FORWARD )
		speedY = shipSpeed * speedY_multiplier;
	else if( shipState & SHIP_BACKWARD )
			speedY = -shipSpeed * speedY_multiplier;
		 else
			speedY = 0;

	if( shipState & SHIP_LEFT )
		speedX = -shipSpeed * speedX_multiplier;
	else if( shipState & SHIP_RIGHT )
			speedX = shipSpeed * speedX_multiplier;
		 else
			speedX = 0;

	//In case of enemy y increases normally
	speedY *= -1;

	Object::loop();

	weapon.loopProjectiles();

	if(getColRect().x < 0)
	{
		x = - collisionRect.x;
		unsetLeft();
		timer = 0;
	}

	if(getColRect().x > WND_WIDTH - getColRect().w)
	{
		x = (float) WND_WIDTH - getColRect().w - collisionRect.x;
		unsetRight();
		timer = 0;
	}

	//Check collision with player
	Ship* pShip = SpaceShooter::pGame->getPlayerShip();

	SDL_Rect pShipRect; //player ship
	SDL_Rect shipRect; //this ship

	pShipRect = pShip->getColRect();
	shipRect = getColRect();

	if( Util::rectCollided(pShipRect, shipRect) )
	{
		pShip->takeDamage( shipHealth ); //Take damage in function of shipHealth
		shipHealth = 0;					 //Ship will be removed
	}
}

void Enemy::render(SDL_Surface* destSurface){

	Object::render(destSurface);

	weapon.renderProjectiles(destSurface);
}

void Enemy::moveEnemy(){
	
	switch(eShipType)
	{
		case ESHIP_1:

			if(shipState == SHIP_FORWARD)
			{
				if(y >= 40)
					if( rand() % 10 < 5 )
						setRight();
					else
						setLeft();
			}
			else
			{
				if(speedX == 0)
				{
					if(x <= WND_WIDTH / 2)
						setRight();
					else
						setLeft();
				}
			}

		break;

		case ESHIP_2:
			
			if( shipState == SHIP_FORWARD )
			{
				if(y > 20 && SDL_GetTicks() >= timer + 800)
				{
					timer = SDL_GetTicks();

					if(rand() % 10 < 4)
						if(rand() % 10 < 5)
							setRight();
						else
							setLeft();
				}
			}
			else
			{
				if(SDL_GetTicks() >= timer + 3000)
				{
					shipState = SHIP_FORWARD;
					timer = SDL_GetTicks();
				}
			}

		break;
	}
}
